import edu.udo.cs.wvtool.wordlist.WVTWordList;
import net.sf.javaml.classification.Classifier;
import net.sf.javaml.core.DenseInstance;
import net.sf.javaml.core.Instance;
import net.sf.javaml.tools.weka.WekaClassifier;
import weka.classifiers.functions.SMO;
import weka.classifiers.trees.J48;

import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: tobias
 * Date: 5/5/11
 * Time: 8:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    //
    /* 3 examples of input:

     -process /home/tobias/Skola/AI/20_newsgroups_small
     -classify vectorsfull wordsfull 51139 kNN 1
     -eval vectorsfull wordsfull kNN 3

     */


    public static void main(String[] args) {

        try {
            BaseModule bm = new BaseModule();
            if(args[0].equals("-process")) {
                String dir = args[1];
                bm.parseDocuments(dir);
                WVTWordList wordlist = bm.createWordList();
                wordlist.pruneByFrequency(10, 80);
                DataSet data = bm.createVectors(wordlist, 200);
                if(args.length > 2 && args[2].equals("-o")) {
                    data.store(new File(args[3]), new File(args[4]));
                }
                else {
                    data.store(new File("vectors2"), new File("words2"));
                }
            } else if(args[0].equals("-classify")){

                String dirT = args[1];
                String dirW = args[2];
                String dirD = args[3];
                DataSet data = bm.loadDataSet(new File(dirT), new File(dirW));
                Classifier classifier;
                if(args[4].equals("kNN")) {
                    int numNeig = new Integer(args[5]);
                    classifier = new MyKNN(numNeig);
                }
                else if(args[4].equals("weka_SMO")) {
                    SMO smo = new SMO();
                    classifier = new WekaClassifier(smo);
                }
                else if(args[4].equals("weka_J48")) {
                    J48 j48 = new J48();
                    classifier = new WekaClassifier(j48);
                }
                else if(args[4].equals("bayes")) {
                    classifier = new Bayes();
                }
                else {
                    classifier = null;
                    System.exit(99);
                }
                classifier.buildClassifier(data);
                for(Instance i : data)
                    System.out.println(classifier.classify(i));
                DenseInstance instance = bm.createVector(data, dirD);
                instance.setClassValue("/home/tobias/Skola/AI/20_newsgroups_small/comp.windows.x");
                Object prediction = classifier.classify(instance);
                System.out.println(prediction);
            }
            else if(args[0].equals("-eval")) {

                String dirT = args[1];
                String dirW = args[2];
                DataSet data = bm.loadDataSet(new File(dirT), new File(dirW));
                Classifier classifier;
                if(args[3].equals("kNN")) {
                    int numNeig = new Integer(args[4]);
                    classifier = new MyKNN(numNeig);
                }
                else if(args[3].equals("weka_SMO")) {
                    SMO smo = new SMO();
                    classifier = new WekaClassifier(smo);
                }
                else if(args[3].equals("bayes")) {
                    classifier = new Bayes();
                }
                else if(args[3].equals("weka_J48")) {
                    J48 j48 = new J48();
                    classifier = new WekaClassifier(j48);
                }
                else {
                    classifier = null;
                    System.exit(99);
                }
                Evaluate.evaluate(classifier, data);
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    }

